﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WebHoiChoMuaBan.Models;

namespace WebHoiChoMuaBan.Controllers.Helpers
{
    public class CategoryHelper : HelperBase
    {
        public struct SubCategory
        {
            public int? id;
            public string name;
        }

        public CategoryHelper()
            : base()
        {

        }

        //Ham nay dung de lay 1 doi tuong mac dinh 
        //dung de phan loai nhom san pham trong trang productlist.aspx
        public int getRandomCategory()
        {
            return (from n in this.dataContext.Categories
                    where n.PageId == null
                    select n).ToList().First().CateId;
        }

        //Ham nay dung de lay 12 Category mac dinh
        //dung de hien thi link phan loai danh muc trong trang productlist.aspx
        public List<Category> getDefaultCategory(int numOfCategory)
        {
            return (from n in this.dataContext.Categories
                    where n.PageId == null
                    select n).Take(numOfCategory).ToList();
        }

        public List<Category> getMemberCategory(int? uid)
        {
            Member mem = (from e in this.dataContext.Members where e.MemId == uid select e).SingleOrDefault();
            string[] catId = mem.MemCategoryList.Split(',');
            List<int> listCate = new List<int>();
            for (int i = 0; i < catId.Length; i++)
            {
                try
                {
                    listCate.Add(int.Parse(catId[i]));
                }
                catch
                {
                }
            }

            List<Category> result = (from e in this.dataContext.Categories where (listCate.Contains(e.CateId)) select e).ToList<Category>();
            return result;
        }

           public List<Category> getChildFromParrent(List<Category> lstCat)
        {
            List<Category> lstChildlist = new List<Category>();
            foreach(Category cat in lstCat)
                {
                    List<Category> tmp = (from e in base.dataContext.Categories
                        where e.ParentCateId == cat.CateId
                        select e).ToList<Category>();
                    if (tmp.Count != 0)
                    {
                        foreach (Category c in tmp)
                        {
                            lstChildlist.Add(c);
                        }
                    }
                }
           
            return lstChildlist;
        }

        public List<Category> getCates(int? pageId)
        {
            List<Category> categories = null;
            if (pageId != 0)
            {
                categories = (from c in this.dataContext.Categories
                              where c.PageId == pageId.Value
                              select c).ToList();
            }
            else if ((pageId == 0)) //Load het cac danh muc cha
                categories = (from c in this.dataContext.Categories
                              where c.PageId == null
                              select c).ToList();
            return categories;
        }

        public Category getCate(int cateId)
        {
            if (cateId <= 0)
                return null;
            try
            {
                Category category = (from c in this.dataContext.Categories
                                     where c.CateId == cateId
                                     select c).First();
                return category;
            }
            catch
            {
                return null;
            }
        }

        public List<Category> getAllParentCate()
        {
            var cate = (from c in this.dataContext.Categories
                        where c.ParentCateId == null
                        select c);
            return cate.ToList();
        }

        public List<Category> GetParentCategoryByPageId(int? pageId)
        {
            var cate = (from c in this.dataContext.Categories
                        where c.PageId == pageId && c.ParentCateId == null
                        select c);
            return cate.ToList();
        }

        public List<Category> GetChildCategoryById(int parentCateId)
        {
            var cate = (from c in this.dataContext.Categories
                        where c.ParentCateId == parentCateId
                        select c);
            return cate.ToList();
        }

        public List<Category> GetListParentCategory()
        {
            var cate = from c in this.dataContext.Categories
                       where c.ParentCateId == null
                       select c;
            return cate.ToList();
        }

        public List<Category> GetListParentCategoryPagging(int page)
        {
            var cate = from c in this.dataContext.Categories
                       where c.ParentCateId == null
                       select c;
            List<Category> CateList = cate.ToList<Category>();
            List<Category> result = CateList.Skip((page - 1) * 9).Take(9).ToList();

            //int totalPages = (int)Math.Ceiling((double)CateList.Count() / 9);
            return result;
        }


        public List<Category> getListParrentCategory(List<int?> childCat)
        {
            List<Category> result = new List<Category>();
            for (int i = 0; i < childCat.Count; i++)
            {
                Category parrCat = (from e in this.dataContext.Categories where e.CateId == childCat[i] select e).SingleOrDefault();
                result.Add(parrCat);
            }
            return result;
        }

        public Category GetCategoryById(int id)
        {
            return this.dataContext.Categories.SingleOrDefault(m => m.CateId == id);
        }

        //
        // Insert/Delete Methods
        public void Add(Category cate)
        {
            this.dataContext.Categories.InsertOnSubmit(cate);
            Save();
        }

        public string Delete(int cateId)
        {
            Category cate = (from e in this.dataContext.Categories where e.CateId == cateId select e).SingleOrDefault();
            // Xóa menu cấp 2, xóa trực tiếp
            if (cate.ParentCateId != null)
            {
                try
                {
                    this.dataContext.Categories.DeleteOnSubmit(cate);
                    Save();
                    return "done@" + cate.ParentCateId;
                }
                catch
                {
                    return "product";
                }
            }
            else
            {
                //xóa menu cấp 1
                try
                {
                    this.dataContext.Categories.DeleteOnSubmit(cate);
                    Save();
                    return "done@" + cate.PageId;
                }
                catch
                {
                    return "category";
                }
            }
        }

        public void Update(Category cate)
        {
            Category cat = getCate(cate.CateId);
            cat.CateCount = cate.CateCount;
            cat.CateIcon = cate.CateIcon;
            cat.CateName = cate.CateName;
            cat.PageId = cate.PageId;
            cat.ParentCateId = cate.ParentCateId;
            Save();
        }

        public List<Category> getAllParrentNewsCategory()
        {
            List<Category> result = (from e in this.dataContext.Categories where e.PageType.Template.TemFilePath.ToLower().Equals("new") select e).ToList<Category>();
            return result;
        }

        public List<Category> getAllParrentProductCategory()
        {
            List<Category> result = (from e in this.dataContext.Categories where (!e.PageType.Template.TemFilePath.ToLower().Equals("new")) select e).ToList<Category>();
            return result;
        }

        public List<Category> getAllParrentProductCategoryByTemplate(List<Template> temp)
        {
            List<Category> result = new List<Category>();
            if (temp.Count == 1 && temp[0] == null)
            {
                result = getAllParrentProductCategory();
                return result;
            }
            foreach (Template t in temp)
            {
                List<Category> cat = (from e in this.dataContext.Categories where (e.PageType.Template.TemId == t.TemId) select e).ToList<Category>();
                if (cat != null)
                {
                    foreach (Category c in cat)
                    {
                        result.Add(c);
                    }
                }
            }
            return result;
        }

        //
        // Persistence
        public void Save()
        {
            this.dataContext.SubmitChanges();
        }
    }
}